package com.idopartx.phonelightning.utils;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.idopartx.phonelightning.R;
import com.idopartx.phonelightning.entity.ScreenLightningEntity;
import com.idopartx.phonelightning.widget.screen.BreathView;
import com.idopartx.phonelightning.widget.screen.LinePathView;
import com.idopartx.phonelightning.widget.screen.RoundedDashView;
import com.idopartx.phonelightning.entity.ScreenLightningEntity;
import com.idopartx.phonelightning.widget.screen.BreathView;
import com.idopartx.phonelightning.widget.screen.LinePathView;

/**
 * 来电监听窗口工具类
 *
 * @author : Joh Liu
 * @date : 2019/8/15 11:14
 */
public class WindowsUtils {
    private static final String LOG_TAG = "WindowUtils";
    private static View mView = null;
    private static WindowManager mWindowManager = null;
    private static Context mContext = null;
    public static Boolean isShown = false;

    /**
     * 显示弹出框
     *
     * @param context
     */
    public static void showPopupWindow(final Context context, ScreenLightningEntity screenVo) {
        if (isShown) {
            return;
        }
        isShown = true;
        // 获取应用的Context
        mContext = context.getApplicationContext();
        // 获取WindowManager
        mWindowManager = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);


        mView = setUpView(context, screenVo);
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();

        params.x = 0;
        params.y = 0;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            // 注意TYPE_SYSTEM_ALERT从Android8.0开始被舍弃了
            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        } else {
            // 从Android8.0开始悬浮窗要使用TYPE_APPLICATION_OVERLAY
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
        params.gravity = Gravity.CENTER;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        params.format = PixelFormat.RGBA_8888;
        mWindowManager.addView(mView, params);
    }

    public static int dp2px(final float dpValue) {
        float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 隐藏弹出框
     */
    public static void hidePopupWindow() {
        if (isShown && null != mView) {
            mWindowManager.removeView(mView);
            isShown = false;
        }
    }

    private static View setUpView(Context context, ScreenLightningEntity screenVo) {
        View view = LayoutInflater.from(context).inflate(R.layout.window_screen_lightning, null);

        LinePathView linePathView = view.findViewById(R.id.view_line);
        RoundedDashView dashView = view.findViewById(R.id.view_dash);
        BreathView breathView = view.findViewById(R.id.view_breath);

        if (screenVo.getType() != null) {
            switch (screenVo.getType()) {
                case 0:
                    linePathView.setType(0);
                    linePathView.setSpeed(screenVo.getSpeed());
                    linePathView.setLineWidth(screenVo.getWidth());
                    linePathView.setLineLength(screenVo.getLength());
                    linePathView.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    linePathView.setSpeed(screenVo.getSpeed());
                    linePathView.setLineWidth(screenVo.getWidth());
                    linePathView.setLineLength(screenVo.getLength());
                    linePathView.setType(1);
                    linePathView.setVisibility(View.VISIBLE);
                    break;


                case 2:
                    dashView.setDashRadius(screenVo.getWidth());
                    dashView.setTranslateSpeed(screenVo.getSpeed());
                    dashView.setVisibility(View.VISIBLE);
                    break;

                case 3:

                    breathView.setSpeed(screenVo.getSpeed());
                    breathView.setBreathWidth(screenVo.getWidth());
                    breathView.setColor(screenVo.getColor());
                    breathView.setVisibility(View.VISIBLE);
                    break;

            }
        }


        view.setOnClickListener(null);
        return view;
    }
}
